<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
	
class Pdf Extends MY_Controller
{
	private $__paid_fees;
	private $__all_fees;
	private $gperiods_value;
	private $student_name;
	private $student_idno;
	private $student_level;
	
	public function __construct()
	{
		parent::__construct();
		$this->page_auth->secure_output_page();
		$this->load->library(array('mpdf'));
		$this->load->model('M_enrollees');
		$this->load->helper('misc');
		$this->gperiods_value	= $this->M_gperiods->get_current_gperiods_value('gp_desc');
		$this->student_name = $this->session->flashdata('student_name');
		$this->student_idno = $this->session->flashdata('student_idno');
		$this->student_level = $this->session->flashdata('student_level');
	}
	
	public function index()
	{
		show_404();
	}
	

	private function __create_pdf($html,$filename = FALSE)
	{
		if($filename == FALSE)
		{
			$filename = str_replace(' ','_',trim($this->student_name)).'_'.trim($this->student_idno);
		}
		$mpdf=new mPDF('c','A4','','' , 10 , 10 , 10 , 10 , 0 , 0);
		$mpdf->SetDisplayMode('fullpage');
		$mpdf->list_indent_first_level = 0;  // 1 or 0 - whether to indent the first level of a list
		$mpdf->WriteHTML($html);
		$mpdf->Output($filename,'I');
	}
	
	public function print_enrollment_list($x = FALSE)
	{
		$x = htmlspecialchars(trim($x));
		$this->load->model('M_search','ms');
		if($x == FALSE or $x == 'all')
		{
			$data['enrollmentlist'] = $this->ms->verified_enrollees_for_enrollmentlist();
		}else{
			$data['enrollmentlist'] = $this->ms->verified_enrollees_for_enrollmentlist($x);
		}
		$data['print'] = TRUE;
		
		$html = $this->load->view('search/enrollmentlist',$data,TRUE);
		$this->__create_pdf($html);
	}
	
	public function monthlyreports($id)
	{
		$this->load->model('M_finance','mf');
		
		$result = $this->mf->generate_monthly_report();
	}
	
	public function registration($id = FALSE,$auth = FALSE)
	{
		if($id == FALSE AND $auth == FALSE AND !is_numeric($id))
		{
			show_404();
		}else{
			$this->load->model(array('M_school_settings','M_student'));
			$query = $this->M_school_settings->get_all_school_setting();
			$student = $this->M_student->get_student($id);
			if(!empty($query) AND !empty($student))
			{
				$data['school_name'] = $query->school_name;
				$data['school_address'] = $query->school_address;
				$data['school_telephone'] = $query->school_telephone;
				$data['school_email'] = $query->school_email;
				$data['student'] = $student;
				
				$html = $this->load->view('legalforms/form_registration',$data,TRUE);
				
				$this->load->library('mpdf');
				$filename = 'Registration Form: '.ucfirst($student->firstname.' '.$student->middlename.' '.$student->lastname);
				$mpdf=new mPDF('c','LETTER','','' , 10 , 10 , 10 , 10 , 0 , 0);
				$mpdf->SetDisplayMode('fullpage');
				$mpdf->AddPage('L');
				$mpdf->list_indent_first_level = 0;  // 1 or 0 - whether to indent the first level of a list
				$mpdf->WriteHTML($html);
				$mpdf->Output($filename,'I');
			}else{
				
			}
		}
	}
	
	public function assessment_fee($id = FALSE,$auth = FALSE)
	{
		if($id == FALSE AND $auth == FALSE AND !is_numeric($id))
		{
			show_404();
		}else{
			$this->load->model(array('M_school_settings','M_student','M_enrollees','M_assessment_fees'));
			$query = $this->M_school_settings->get_all_school_setting();
			$student = $this->M_student->get_student($id);
			$af = $this->M_assessment_fees->print_assessment_fee($id);
			if(!empty($query) AND !empty($student))
			{
				$data['school_name'] = $query->school_name;
				$data['school_address'] = $query->school_address;
				$data['school_telephone'] = $query->school_telephone;
				$data['school_mobile'] = $query->school_mobile;
				$data['school_website'] = $query->school_website;
				$data['school_email'] = $query->school_email;
				$data['student'] = $student;
				$data['af'] = $af;
				
				$html = $this->load->view('legalforms/assessment_fee',$data,TRUE);
				
				$this->load->library('mpdf');
				$filename = 'New Student Assessment & Application Form: '.ucfirst($student->firstname.' '.$student->middlename.' '.$student->lastname);
				$mpdf=new mPDF('c','LETTER','','' , 20 , 20 , 10 , 0 , 0 , 0);
				$mpdf->SetDisplayMode('fullpage');
				$mpdf->list_indent_first_level = 0;  // 1 or 0 - whether to indent the first level of a list
				$mpdf->WriteHTML($html);
				$mpdf->Output($filename,'I');
			}else{
				
			}
		}
	}
	
	public function assessment_fee_form($id = FALSE,$auth = FALSE)
	{
		if($id == FALSE AND $auth == FALSE AND !is_numeric($id))
		{
			show_404();
		}else{
			$this->load->model(array('M_school_settings','M_student','M_enrollees','M_assessment_fees','M_pre_applicants','M_fees'));
			$query = $this->M_school_settings->get_all_school_setting();
			$student = $this->M_student->get_student($id);
			$inquiry = $this->M_pre_applicants->get_applicants($id);
			$af = $this->M_assessment_fees->print_assessment_fee($id);
			$feedata = $this->M_fees->get_assessment_fees_by_grade_level();
			if(!empty($query) AND !empty($student))
			{
				$data['school_name'] = $query->school_name;
				$data['school_address'] = $query->school_address;
				$data['school_telephone'] = $query->school_telephone;
				$data['school_mobile'] = $query->school_mobile;
				$data['school_website'] = $query->school_website;
				$data['school_email'] = $query->school_email;
				$data['student'] = $student;
				$data['inquiry'] = $inquiry;
				$data['af'] = $af;
				$data['feedata'] = $feedata;
				
				$html = $this->load->view('legalforms/assessment_fee_form',$data,TRUE);
				
				$this->load->library('mpdf');
				$filename = 'New Student Assessment & Application Form: '.ucwords(strtolower($student->p_a_firstname)).' '.ucwords(strtolower($student->p_a_middlename)).' '.ucwords(strtolower($student->p_a_lastname)).' '.strtolower($student->name_ext);
				$mpdf=new mPDF('c','LETTER','','' , 20 , 20 , 10 , 0 , 0 , 0);
				$mpdf->SetDisplayMode('fullpage');
				$mpdf->list_indent_first_level = 0;  // 1 or 0 - whether to indent the first level of a list
				$mpdf->WriteHTML($html);
				$mpdf->Output($filename,'I');
			}else{
				show_404();
			}
		}
	}

	public function assessment_or($id = FALSE,$auth = FALSE)
	{
		if($id == FALSE AND $auth == FALSE AND !is_numeric($id))
		{
			show_404();
		}else{
			$this->load->model(array('M_school_settings','M_student','M_enrollees','M_assessment_fees','M_pre_applicants','M_fees','M_applicants'));
			$query = $this->M_school_settings->get_all_school_setting();
			$student = $this->M_student->get_student($id);
			$payee = $this->M_applicants->get_payee($student->profile_id);
			$inquiry = $this->M_pre_applicants->get_applicants($id);
			$af = $this->M_assessment_fees->print_assessment_fee($id);
			$feedata = $this->M_fees->get_assessment_fees_by_grade_level();
			if(!empty($query) AND !empty($student))
			{
				$data['school_name'] = $query->school_name;
				$data['school_address'] = $query->school_address;
				$data['school_telephone'] = $query->school_telephone;
				$data['school_mobile'] = $query->school_mobile;
				$data['school_website'] = $query->school_website;
				$data['school_email'] = $query->school_email;
				$data['student'] = $student;
				$data['payee'] = $payee;
				$data['inquiry'] = $inquiry;
				$data['af'] = $af;
				$data['feedata'] = $feedata;
				$data['dater'] = NOW;
				
				$html = $this->load->view('legalforms/assessment_or',$data,TRUE);
				
				$this->load->library('mpdf');
				$filename = 'Assessment OR: '.ucwords(strtolower($student->firstname)).' '.ucwords(strtolower($student->middlename)).' '.ucwords(strtolower($student->lastname)).' '.strtolower($student->name_ext);
				$mpdf=new mPDF('c','LETTER','','' , 0 , 0 , 0 , 0 , 0 , 0);
				$mpdf->SetDisplayMode('fullpage');
				$mpdf->list_indent_first_level = 0;  // 1 or 0 - whether to indent the first level of a list
				$mpdf->WriteHTML($html);
				$mpdf->Output($filename,'I');
			}else{
				
			}
		}
	}

	public function payment_record_or($id = FALSE,$auth = FALSE)
	{
		if($id == FALSE AND $auth == FALSE AND !is_numeric($id))
		{
			show_404();
		}else{
			$this->load->model(array('M_school_settings','M_student','M_enrollees','M_assessment_fees','M_pre_applicants','M_fees','M_student_payment_record','M_student_payment_breakdown','M_payee'));
			$query = $this->M_school_settings->get_all_school_setting();
			//$student = $this->M_student->get_student($id);
			//$inquiry = $this->M_pre_applicants->get_applicants($id);
			//$af = $this->M_assessment_fees->print_assessment_fee($id);
			//$feedata = $this->M_fees->get_assessment_fees_by_grade_level();
			$payment_record = $this->M_student_payment_record->print_payment_record($id);
			$payee = $this->M_payee->get_payee($payment_record->e_profile_id);
			$num_payment_record = $this->M_student_payment_breakdown->count_payment_breakdown_by_spr_id($id);
			$payment_record_breakdown_1 = $this->M_student_payment_breakdown->get_payment_breakdown_by_spr_id_with($id,0,13);
			$payment_record_breakdown_2 = $this->M_student_payment_breakdown->get_payment_breakdown_by_spr_id_with($id,13,$num_payment_record->Max_num);
			if(!empty($query) AND !empty($payment_record))
			{
				$data['school_name'] = $query->school_name;
				$data['school_address'] = $query->school_address;
				$data['school_telephone'] = $query->school_telephone;
				$data['school_mobile'] = $query->school_mobile;
				$data['school_website'] = $query->school_website;
				$data['school_email'] = $query->school_email;
				//$data['student'] = $student;
				//$data['inquiry'] = $inquiry;
				//$data['af'] = $af;
				//$data['feedata'] = $feedata;
				$data['payee'] = $payee;
				$data['payment_record'] = $payment_record;
				$data['payment_record_breakdown_1'] = $payment_record_breakdown_1;
				$data['payment_record_breakdown_2'] = $payment_record_breakdown_2;
				$data['dater'] = NOW;
				
				$html = $this->load->view('legalforms/payment_record_or',$data,TRUE);
				
				$this->load->library('mpdf');
				$filename = 'Payment Record OR: '.ucwords(strtolower($payment_record->firstname)).' '.ucwords(strtolower($payment_record->middlename)).' '.ucwords(strtolower($payment_record->lastname)).' '.ucwords(strtolower($payment_record->name_ext));
				$mpdf=new mPDF('c','LETTER','','' , 0 , 0 , 0 , 0 , 0 , 0);
				$mpdf->SetDisplayMode('fullpage');
				$mpdf->list_indent_first_level = 0;  // 1 or 0 - whether to indent the first level of a list
				$mpdf->WriteHTML($html);
				$mpdf->Output($filename,'I');
			}else{
				
			}
		}
	}
	
	public function print_reservation_or($ror,$id)
	{
		if($id == FALSE AND $auth == FALSE AND !is_numeric($ror))
		{
			show_404();
		}else{
			$this->load->model(array('M_school_settings','M_student','M_enrollees','M_assessment_fees','M_pre_applicants','M_fees','M_student_payment_record','M_student_payment_breakdown','M_payee'));
			$query = $this->M_school_settings->get_all_school_setting();
			$payment_record = $this->M_student_payment_record->print_payment_record($ror);
			$payee = $this->M_payee->get_payee($payment_record->e_profile_id);
			if(!empty($query) AND !empty($payment_record))
			{
				$data['school_name'] = $query->school_name;
				$data['school_address'] = $query->school_address;
				$data['school_telephone'] = $query->school_telephone;
				$data['school_mobile'] = $query->school_mobile;
				$data['school_website'] = $query->school_website;
				$data['school_email'] = $query->school_email;
				$data['payment_record'] = $payment_record;
				$data['payee'] = $payee;
				$data['dater'] = NOW;
				
				$html = $this->load->view('legalforms/payment_reservation_or',$data,TRUE);
				
				$this->load->library('mpdf');
				$filename = 'Payment Reservation OR: '.ucwords(strtolower($payment_record->firstname)).' '.ucwords(strtolower($payment_record->middlename)).' '.ucwords(strtolower($payment_record->lastname)).' '.strtolower($payment_record->name_ext);
				$mpdf=new mPDF('c','LETTER','','' , 0 , 0 , 0 , 0 , 0 , 0);
				$mpdf->SetDisplayMode('fullpage');
				$mpdf->list_indent_first_level = 0;  // 1 or 0 - whether to indent the first level of a list
				$mpdf->WriteHTML($html);
				$mpdf->Output($filename,'I');
			}else{
				
			}
		}
	}

	public function print_scedule_installment_plan($id = FALSE)
	{
		var_dump($id);
		if($id == FALSE AND !is_numeric($id)){
			show_404();
		}else{
			$this->load->model(array('M_school_settings','M_student_payment_record','M_student_payment_schedule','M_enrollees'));
			$query = $this->M_school_settings->get_all_school_setting();
			$payment_value = $this->M_enrollees->get_student_payment_mode($id);
			$profiles = $this->M_student_payment_schedule->get_student_profile($id);
			$payment_plan_value = $this->M_student_payment_schedule->get_payment_mode_value($profiles->e_grade_level,$profiles->e_ay_id);
			if(!empty($query) AND !empty($profiles))
			{
				$data['school_name'] = $query->school_name;
				$data['school_address'] = $query->school_address;
				$data['school_telephone'] = $query->school_telephone;
				$data['school_mobile'] = $query->school_mobile;
				$data['school_website'] = $query->school_website;
				$data['school_email'] = $query->school_email;
				$data['payment_value'] = $payment_value;
				$data['profiles'] = $profiles;
				$data['payment_plan_value'] = $payment_plan_value;
				$data['dater'] = NOW;
				
				$html = $this->load->view('legalforms/print_schedule_installment_plan',$data,TRUE);
				
				$this->load->library('mpdf');
				$filename = 'Schedule Of Installment Plan Due Dates: '.ucwords(strtolower($profiles->firstname)).' '.ucwords(strtolower($profiles->middlename)).' '.ucwords(strtolower($profiles->lastname)).' '.ucwords(strtolower($profiles->name_ext));
				$mpdf=new mPDF('c','LETTER','','' , 0 , 0 , 0 , 0 , 0 , 0);
				$mpdf->SetDisplayMode('fullpage');
				$mpdf->list_indent_first_level = 0;  // 1 or 0 - whether to indent the first level of a list
				$mpdf->WriteHTML($html);
				$mpdf->Output($filename,'I');
			}else{
				show_404();
			}
		}
	}

	public function print_waiver($id = FALSE)
	{
		if($id == FALSE AND $auth == FALSE AND !is_numeric($id))
		{
			show_404();
		}else{
			$this->load->model(array('M_school_settings','M_student','M_enrollees','M_assessment_fees','M_pre_applicants','M_fees'));
			$query = $this->M_school_settings->get_all_school_setting();
			$student = $this->M_student->get_student($id);
			//$inquiry = $this->M_pre_applicants->get_applicants($id);
			//$af = $this->M_assessment_fees->print_assessment_fee($id);
			//$feedata = $this->M_fees->get_assessment_fees_by_grade_level();
			$file_needed = $this->M_enrollees->check_needed_files($id);
			
			$file_me = array();
			if($file_needed->nationality  == 'Filipino'){
				if($file_needed->form137a == NULL || $file_needed->form137a == 0){
					$file_me[] = 'Report Card (F-138) or authentic copy of latest academic record';
				}
				if($file_needed->reccomendationform == NULL || $file_needed->reccomendationform == 0){
					$file_me[] = 'Recommendation Letter';
				}
				if($file_needed->birthcertificate == NULL || $file_needed->birthcertificate == 0){
					$file_me[] = 'Copy of Birth Certificate';
				}
				if($file_needed->medicalcertificate == NULL || $file_needed->medicalcertificate == 0){
					$file_me[] = 'Medical Certificate';
				}
				if($file_needed->idpicture2x2 == NULL || $file_needed->idpicture2x2 == 0){
					$file_me[] = '3 copies of 2x2 ID Picture';
				}
			}else{
				if($file_needed->form137a == NULL || $file_needed->form137a == 0){
					$file_me[] = 'Report Card (F-138) or authentic copy of latest academic record';
				}
				if($file_needed->reccomendationform == NULL || $file_needed->reccomendationform == 0){
					$file_me[] = 'Recommendation Letter';
				}
				if($file_needed->birthcertificate == NULL || $file_needed->birthcertificate == 0){
					$file_me[] = 'Copy of Birth Certificate';
				}
				if($file_needed->medicalcertificate == NULL || $file_needed->medicalcertificate == 0){
					$file_me[] = 'Medical Certificate';
				}
				if($file_needed->idpicture2x2 == NULL || $file_needed->idpicture2x2 == 0){
					$file_me[] = '3 copies of 2x2 ID Picture';
				}
				if($file_needed->passport == NULL || $file_needed->passport == 0){
					$file_me[] = 'Copy of Passport';
				}
				if($file_needed->visa == NULL || $file_needed->visa == 0){
					$file_me[] = 'Copy of Visa';
				}
				if($file_needed->int_icr == NULL || $file_needed->int_icr == 0){
					$file_me[] = 'Copy of ACR-icard';
				}
				if($file_needed->int_specialpermit == NULL || $file_needed->int_specialpermit == 0){
					$file_me[] = 'Special Study Permit';
				}
			}

			if(!empty($query) AND !empty($student))
			{
				$data['school_name'] = $query->school_name;
				$data['school_address'] = $query->school_address;
				$data['school_telephone'] = $query->school_telephone;
				$data['school_mobile'] = $query->school_mobile;
				$data['school_website'] = $query->school_website;
				$data['school_email'] = $query->school_email;
				$data['student'] = $student;
				//$data['inquiry'] = $inquiry;
				//$data['af'] = $af;
				//$data['feedata'] = $feedata;
				$data['dater'] = NOW;
				if(!empty($file_me)){
					$data['files'] = $file_me;
				}

				$html = $this->load->view('legalforms/waiver',$data,TRUE);
				
				$this->load->library('mpdf');
				$filename = 'Waiver: '.ucwords(strtolower($student->firstname)).' '.ucwords(strtolower($student->middlename)).' '.ucwords(strtolower($student->lastname));
				$mpdf=new mPDF('c','LETTER','','' , 0 , 0 , 0 , 0 , 0 , 0);
				$mpdf->SetDisplayMode('fullpage');
				$mpdf->list_indent_first_level = 0;  // 1 or 0 - whether to indent the first level of a list
				$mpdf->WriteHTML($html);
				$mpdf->Output($filename,'I');
			}else{
				
			}
		}
	}

	public function enrollment_transaction($id = FALSE,$auth = FALSE)
	{
		if($id == FALSE AND $auth == FALSE AND !is_numeric($id))
		{
			show_404();
		}else{
			$this->load->model(array('M_school_settings','M_student','M_enrollment_transactions'));
			$query = $this->M_school_settings->get_all_school_setting();
			$student = $this->M_student->get_student($id);
			$e_transaction = $this->M_enrollment_transactions->get_enrollment_transaction($id);
			if(!empty($query) AND !empty($student) AND !empty($e_transaction))
			{
				$data['school_name'] = $query->school_name;
				$data['school_address'] = $query->school_address;
				$data['school_telephone'] = $query->school_telephone;
				$data['school_email'] = $query->school_email;
				$data['student'] = $student;
				$data['e_transaction'] = $e_transaction;
				$data['school_year'] = $this->schoolyear_date;

				$html = $this->load->view('legalforms/enrollment_transaction',$data,TRUE);
				
				$this->load->library('mpdf');
				$filename = 'Enrollment Transaction Slip: '.ucfirst($student->firstname.' '.$student->middlename.' '.$student->lastname);
				$mpdf=new mPDF('c','LETTER','','' , 0 , 0 , 0 , 0 , 0 , 0);
				$mpdf->SetDisplayMode('fullpage');
				$mpdf->list_indent_first_level = 0;  // 1 or 0 - whether to indent the first level of a list
				$mpdf->WriteHTML($html);
				$mpdf->Output($filename,'I');
			}else{
				
			}
		}
	}
	
	public function entrance_test_result($id = FALSE,$auth = FALSE)
	{
		if($id == FALSE AND $auth == FALSE AND !is_numeric($id))
		{
			show_404();
		}else{
			$this->load->model(array('M_school_settings','M_student','M_entrance_test_results'));
			$query = $this->M_school_settings->get_all_school_setting();
			$student = $this->M_student->get_student($id);
			$test_result = $this->M_entrance_test_results->get_entrance_test_result($id);
			if(!empty($query) AND !empty($student))
			{
				$data['school_name'] = $query->school_name;
				$data['school_address'] = $query->school_address;
				$data['school_telephone'] = $query->school_telephone;
				$data['school_email'] = $query->school_email;
				$data['student'] = $student;
				$data['test_result'] = $test_result;
				$data['school_year'] = $this->schoolyear_date;
				
				$html = $this->load->view('legalforms/entrance_test_form',$data,TRUE);

				$this->load->library('mpdf');
				$filename = 'Entrance Test Result: '.ucfirst($student->firstname.' '.$student->middlename.' '.$student->lastname);
				$mpdf=new mPDF('c','LETTER','','' , 10 , 10 , 10 , 0 , 0 , 0);
				$mpdf->SetDisplayMode('fullpage');
				$mpdf->list_indent_first_level = 0;  // 1 or 0 - whether to indent the first level of a list
				$mpdf->WriteHTML($html);
				$mpdf->Output($filename,'I');
			}
		}
	}
	
	public function letter_of_acceptance($id = FALSE,$auth = FALSE)
	{
		if($id == FALSE AND $auth == FALSE AND !is_numeric($id))
		{
			show_404();
		}else{
			$this->load->model(array('M_school_settings','M_student'));
			$query = $this->M_school_settings->get_all_school_setting();
			$student = $this->M_student->get_student($id);
			if(!empty($query) AND !empty($student))
			{
				$data['school_name'] = $query->school_name;
				$data['school_address'] = $query->school_address;
				$data['school_telephone'] = $query->school_telephone;
				$data['school_email'] = $query->school_email;
				$data['student'] = $student;
				$data['school_year'] = $this->schoolyear_date;

				$html = $this->load->view('legalforms/letter_of_acceptance',$data,TRUE);
				
				$this->load->library('mpdf');
				$filename = 'Letter Of Acceptance: '.ucfirst($student->firstname.' '.$student->middlename.' '.$student->lastname);
				$mpdf=new mPDF('c','LETTER','','' , 0 , 0 , 0 , 0 , 0 , 0);
				$mpdf->SetDisplayMode('fullpage');
				$mpdf->list_indent_first_level = 0;  // 1 or 0 - whether to indent the first level of a list
				$mpdf->WriteHTML($html);
				$mpdf->Output($filename,'I');
			}else{
				
			}
		}
	}
	
	public function statement_of_responsibilities($id = FALSE,$auth = FALSE)
	{
		if($id == FALSE AND $auth == FALSE AND !is_numeric($id))
		{
			show_404();
		}else{
			$this->load->model(array('M_school_settings','M_student'));
			$query = $this->M_school_settings->get_all_school_setting();
			$student = $this->M_student->get_student($id);
			if(!empty($query) AND !empty($student))
			{
				$data['school_name'] = $query->school_name;
				$data['school_address'] = $query->school_address;
				$data['school_telephone'] = $query->school_telephone;
				$data['school_email'] = $query->school_email;
				$data['student'] = $student;
				$data['school_year'] = $this->schoolyear_date;

				$html = $this->load->view('legalforms/statement_of_responsibilities',$data,TRUE);
				
				$this->load->library('mpdf');
				$filename = 'Statement Of Responsibilites: '.ucfirst($student->firstname.' '.$student->middlename.' '.$student->lastname);
				$mpdf=new mPDF('c','LETTER','','' , 0 , 0 , 0 , 0 , 0 , 0);
				$mpdf->SetDisplayMode('fullpage');
				$mpdf->list_indent_first_level = 0;  // 1 or 0 - whether to indent the first level of a list
				$mpdf->WriteHTML($html);
				$mpdf->Output($filename,'I');
			}else{
				
			}
		}
	}
	
	public function guardian_agreement($id = FALSE,$auth = FALSE)
	{
		if($id == FALSE AND $auth == FALSE AND !is_numeric($id))
		{
			show_404();
		}else{
			$this->load->model(array('M_school_settings','M_student'));
			$query = $this->M_school_settings->get_all_school_setting();
			$student = $this->M_student->get_student($id);
			if(!empty($query) AND !empty($student))
			{
				$data['school_name'] = $query->school_name;
				$data['school_address'] = $query->school_address;
				$data['school_telephone'] = $query->school_telephone;
				$data['school_email'] = $query->school_email;
				$data['student'] = $student;
				$data['school_year'] = $this->schoolyear_date;

				$html = $this->load->view('legalforms/guardian_agreement',$data,TRUE);
				
				$this->load->library('mpdf');
				$filename = 'Guardianship Agreement: '.ucfirst($student->firstname.' '.$student->middlename.' '.$student->lastname);
				$mpdf=new mPDF('c','LETTER','','' , 20 , 20 , 10 , 10 , 10 , 10);
				$mpdf->SetDisplayMode('fullpage');
				$mpdf->list_indent_first_level = 0;  // 1 or 0 - whether to indent the first level of a list
				$mpdf->setHTMLFooter('<div align="right" style="padding-right:10px;">{PAGENO}</div>') ;
				$mpdf->WriteHTML($html);
				$mpdf->Output($filename,'I');
			}else{
				
			}
		}
	}
	
	public function homeschoolagreement($x = '')
	{
		$this->load->model('M_student');
		$student = $this->M_student->get_student($id);
	
		$data['school_name'] = $this->school_name;
		$data['school_address'] = $this->school_address;
		$data['school_telephone'] = $this->school_telephone;
		$data['school_email'] = $this->school_email;
		$data['student_data'] = $this->
		
		$html = $this->load->view('legalforms/form_home_school_agreement',$data,TRUE);
		
		$this->load->library('mpdf');
		$filename = 'Home-School Agreement Form: ';
		$mpdf=new mPDF('c','A4','','' , 20 , 10 , 10 , 10 , 0 , 0);
		$mpdf->SetDisplayMode('fullpage');
		$mpdf->list_indent_first_level = 0;  // 1 or 0 - whether to indent the first level of a list
		$mpdf->WriteHTML($html);
		$mpdf->Output($filename,'I');
	
	}
	
	
	public function monthly_reports($params,$auth)
	{
		$this->load->model('M_finance');
		$r = $this->hs->decrypt($params);

		if($r)
		{
			if($this->authlink->check_hash_make($params,$auth))
			{
				$filename = date('m_d_Y');
				$view = $this->M_finance->daily_month_reports_with_names_for_report($r[0],$r[1]);	
				
				$data['report'] = $view;
				$data['year'] = $r[1];
				$data['month'] = $r[0];
				
				$html = $this->load->view('print_pdf_layouts/_monthly_reports',$data,TRUE);
				$this->__create_pdf($html,$filename,TRUE,TRUE);
				
			}else{
				show_error('Cannot generate report, Invalid Authentication Code.',404,'Auth Error!');
			}
		}else{
			show_error('Invalid arguments was not supplied properly to generate report.',404,'Parameter Error');
		}
	}
	
	public function yearly_reports($id = FALSE,$auth = FALSE)
	{
		$pid = $this->hs->decrypt($id);
		if($pid AND $this->authlink->check_hash_make(date('mdy'),$auth))
		{
			$this->load->model('M_finance','mf');
			$result = $this->mf->yearly_schoolyear_reports($pid[0]);
			$data['report'] = $result;
			$html = $this->load->view('print_pdf_layouts/_yearly_reports',$data,TRUE);
			set_time_limit(0);
			$this->__create_pdf($html,$filename,TRUE,TRUE);
			
		}else{
			$this->_msg('e','Please refresh cache, Token for generating report is expired','finance/reports#yearly_reports');
		}
	}
	
	//print masterlist
	public function sectionmasterlist()
	{
		$this->load->library('printer');

		$this->load->model('M_sections');
		$data['sml'] = $this->M_sections->get_section_masterlist_complete();

		$generated = date('M d, Y - H:i A');
		$filename = 'section_overview-'.time();


		$data['print'] = TRUE;
		$data['school_name'] = $this->school_name;
		$data['school_address'] = $this->school_address;
		$data['school_year'] = $this->schoolyear_date;
		$data['print_title'] = 'Section Masterlist';
		
		$html = $this->load->view('print_pdf_layouts/masterlist',$data,TRUE);

		// echo $html;
		$this->printer->init('','','','',5,5,8,15,4,8)
					 ->footer($footer.' - Generated On '.$generated)
					 ->header('Page {PAGENO} of {nbpg} ')
					 ->generate($html,$filename);
	}
	
	public function printrosters($hash = '')
	{
		$x = $this->hs->decrypt($hash);
		
		if($x)
		{
			if($this->input->post('generate_roster'))
			{
				$level_id = $x[0];
				
				

				$this->load->library('printer');
				$this->load->model(['M_levels','M_sections']);
				$chunk_size =$this->input->post('number_of_students');

				$result = $this->M_sections->get_all_students_from_block_level_by_number($level_id); //all
				
				if($result)
				{
					$p['all_block_students'] = $result;
				}else{
					$p = $this->M_levels->get_sections_and_students_and_block($level_id);
				}
				
				$p['chunk_size'] = $chunk_size;
				$p['school_name'] = $this->school_name;
				$p['school_address'] = $this->school_address;
				$p['school_year'] = $this->schoolyear_date;
				$lvl = $this->M_levels->get_grade_level($level_id);
				$p['print_title'] = ucwords(strtolower($lvl->level_desc)).' Student Block Enrollees by ('.$chunk_size.')';

				$html = $this->load->view('print_pdf_layouts/rosters',$p, TRUE);
				$generated = date('M d, Y - H:i A');
				$filename = 'roster'.date('M-d,-Y_H-i-A');

				$this->printer->init('','','','',5,5,15,15,4,8)
						 ->footer($footer.' - Generated On '.$generated)
						 ->header('Page {PAGENO} of {nbpg} ')
						 ->generate($html,$filename);
				
				exit();

			}else{
				show_404();
			}
		}else{
			show_404();
		}
	}
	
	public function print_foreign($hash = '')
	{
		$x = $this->hs->decrypt($hash);
		if($x)
		{
			$this->load->model('M_levels','ml');
			$this->load->model('M_search','ms');
			$data['ff']['gls'] = $this->ml->get_all_levels();
			$data['ff']['students'] = $this->ms->default_masterlist('foreign');
			
			$data['school_name'] = $this->school_name;
			$data['school_address'] = $this->school_address;
			$data['school_year'] = $this->schoolyear_date;
			$data['print_title'] = 'Foreign Students';
			
			$html = $this->load->view('print_pdf_layouts/_print_masterlist',$data,TRUE);
			set_time_limit(0);
			$this->__create_pdf($html,$filename,TRUE);			
		
		}else{
			show_404();
		}
	}
	
	public function allstudents($type = FALSE,$hash = '')
	{
		$x = $this->hs->decrypt($hash);
		if($x AND $type)
		{
			$this->load->model('M_levels','ml');
			$this->load->model('M_search','ms');
			
			
			$data['school_name'] = $this->school_name;
			$data['school_address'] = $this->school_address;
			$data['school_year'] = $this->schoolyear_date;
			$data['print_title'] = '';
			
			if($type == 'default'){
				$data['ff']['gls'] = $this->ml->get_all_levels();
				$data['ff']['students'] = $this->ms->default_masterlist('default');
				$html = $this->load->view('print_pdf_layouts/_print_masterlist',$data,TRUE);

			$this->__create_pdf($html,$filename);
			}elseif($type == 'format')
			{
				$data['students'] = $this->ms->verified_enrollees_for_enrollmentlist();
				$html = $this->load->view('print_pdf_layouts/_print_enrollmentlist',$data,TRUE);

			$this->__create_pdf($html,$filename,TRUE,FALSE,FALSE,[10,10,20,20,15,15,[215.9,279.4]]);
			}else{
				show_404();
			}
			exit();
		}else{
			show_404();
		}
	}
	
	public function nonforeign($hash = '')
	{
		$x = $this->hs->decrypt($hash);
		if($x)
		{
			$this->load->model('M_levels','ml');
			$this->load->model('M_search','ms');
			$data['ff']['gls'] = $this->ml->get_all_levels();
			$data['ff']['students'] = $this->ms->default_masterlist('nonforeign');
			
			$data['school_name'] = $this->school_name;
			$data['school_address'] = $this->school_address;
			$data['school_year'] = $this->schoolyear_date;
			$data['print_title'] = 'Non Foreign Students';
			
			$html = $this->load->view('print_pdf_layouts/_print_masterlist',$data,TRUE);
			set_time_limit(0);
			$this->__create_pdf($html,$filename,TRUE);
		}else{
			show_404();
		}
	}
	
	public function foreign_data($hash)
	{
		$x = $this->hs->decrypt($hash);
		if($x)
		{
			$this->load->model('M_search','ms');
			$data['students'] = $this->ms->get_foreign_students();
			
			$data['school_name'] = $this->school_name;
			$data['school_address'] = $this->school_address;
			$data['school_year'] = $this->schoolyear_date;
			$data['print_title'] = 'Foreign Students Status';
			
			
			$html = $this->load->view('print_pdf_layouts/_print_foreign_status',$data,TRUE);
			set_time_limit(0);
			$this->__create_pdf($html,$filename,TRUE,FALSE,FALSE,[2,2,5,5,5,5]);	
			// echo $html;

		}else{
			show_404();
		}
	}
	
	public function sectionoverview()
	{
		$this->load->library('printer');
		$this->load->model('M_enrollees','men');
		$this->load->model('M_sections','mse');
		$data['section_master_list'] = $this->mse->get_section_masterlist();
		$data['count'] = function($x,$gender){
						$result = $this->men->by_section_id_and_gender($x,$gender);
						if($result > 0)
						{
							return $result;
						}else{
							return 0;
						}
					 };
		$data['print'] = TRUE;
		$data['school_name'] = $this->school_name;
		$data['school_address'] = $this->school_address;
		$data['school_year'] = $this->schoolyear_date;
		$data['print_title'] = 'Section Overview';

		$generated = date('M d, Y - H:i A');
		$filename = 'section_overview-'.time();

		$html = $this->load->view('master_list/general',$data,TRUE);
		$this->printer->init('','','','',5,5,8,3,4,3)
					 ->footer($footer.' - Generated On '.$generated)
					 ->header('Page {PAGENO} of {nbpg} ')
					 ->generate($html,$filename);
	}
	
	public function sectionsummarry()
	{
		$this->load->library('printer');

		$data['print'] = TRUE;
		$data['school_name'] = $this->school_name;
		$data['school_address'] = $this->school_address;
		$data['school_year'] = $this->schoolyear_date;
		$data['print_title'] = 'Section Overview';

		$this->load->model('M_sections','mse');
		$this->load->model('M_sections','mse');
		$data['section_master_list'] = $this->mse->get_section_masterlist();
		$data['summary'] = $this->mse->get_section_summary();
		
		

		$generated = date('M d, Y - H:i A');
		$filename = 'section_overview-'.time();
		
		$html = $this->load->view('master_list/detailed',$data,TRUE);
		$this->printer->init('','','','',7,7,8,5,4,5)
					 ->footer($footer.' - Generated On '.$generated)
					 ->header('Page {PAGENO} of {nbpg} ')
					 ->generate($html,$filename);
	}
	
	public function balancereports($hash = '')
	{
		$x = $this->hs->decrypt($hash);
		if($x)
		{
			$this->load->library('printer');
			$this->load->model('M_fees','mf');

			$data['result'] = $this->mf->get_balance_report($x[0]);
			$data['sy'] = $this->schoolyear_date;

			$html = $this->load->view('print_pdf_layouts/balancereports',$data,TRUE);

			$generated = date('M d, Y - h:i A');
			$filename = 'balance_report'.date('M-d,-Y_H-i-A');
			$this->printer->init('','','','',5,5,15,15,4,8)
						 ->footer($footer.' - Generated On '.$generated)
						 ->header($this->school_name)
						 ->generate($html,$filename);
			exit();
		}

		show_error('Unable to retrieve the requested file.',404);
	}
	
	public function printables()
	{
		$this->view_data['hash'] = __link(time());
	}
	
	public function classlist($hash = '')
	{
		$x = $this->hs->decrypt($hash);
		if($x)
		{
			$this->load->library('printer');
			$this->load->model('M_sections','sec');
			$sec = $this->sec->get_all_sections();

			$section['school']['school_name'] = $this->school_name;
			$section['school']['school_address'] = $this->school_address;
			$section['school']['school_year'] = $this->schoolyear_date;
			$section['print'] = TRUE;
			
			if($sec)
			{
				foreach ($sec as $key => $value) {
					$section['data'][$key]['data']['section']['male'] = $this->sec->get_all_students_in_section($value->section_id,'male');
					$section['data'][$key]['data']['section']['female'] = $this->sec->get_all_students_in_section($value->section_id,'female');
					$section['data'][$key]['data']['section']['adviser'] = ucwords(strtolower($value->adviser)) ;
					$section['data'][$key]['data']['section']['room_name'] = ucwords(strtolower($value->room_name));
					$section['data'][$key]['data']['section']['room_desc'] = ucwords(strtolower($value->room_desc));
					$section['data'][$key]['data']['section']['section'] = ucwords(strtolower($value->section_name));
					$section['data'][$key]['data']['section']['malecount'] = $this->sec->_get_student_by_section($value->section_id,'male');
					$section['data'][$key]['data']['section']['femalecount'] = $this->sec->_get_student_by_section($value->section_id,'female');
				}
			}

			$html = $this->load->view('print_pdf_layouts/classlist_master',$section,TRUE);

			$generated = date('M d, Y - H:i A');
			$filename = 'classlist_'.date('M-d,-Y_H-i-A');
			$this->printer->init('','','','',5,5,15,15,4,8)
						 ->footer($footer.' - Generated On '.$generated)
						 ->header('Page {PAGENO} of {nbpg} ')
						 ->generate($html,$filename);
			exit();
		}

		show_404();
	}
	
	public function sectionforclasslist($hash = '')
	{	
		$x = $this->hs->decrypt($hash);

		if($x)
		{

			$sec_id = $x[0];
			$this->load->library('printer');
			$this->load->model('M_sections','sec');
			$sec = $this->sec->get_all_sections($sec_id)[0];

			$section['print'] = FALSE;
			$section['school']['school_name'] = $this->school_name;
			$section['school']['school_address'] = $this->school_address;
			$section['school']['school_year'] = $this->schoolyear_date;

			$section['data']['section']['adviser'] = ucwords(strtolower($sec->adviser)) ;
			$section['data']['section']['room_name'] = ucwords(strtolower($sec->room_name));
			$section['data']['section']['room_desc'] = ucwords(strtolower($sec->room_desc));
			$section['data']['section']['section'] = ucwords(strtolower($sec->section_name));
			$section['data']['section']['female'] = $this->sec->get_all_students_in_section($sec_id,'female');
			$section['data']['section']['male'] = $this->sec->get_all_students_in_section($sec_id,'male');
			$section['data']['section']['femalecount'] = $this->sec->_get_student_by_section($sec_id,'female');
			$section['data']['section']['malecount'] = $this->sec->_get_student_by_section($sec_id,'male');



			$html = $this->load->view('print_pdf_layouts/classlist_master',$section,TRUE);
			// echo $html;
			// exit();
			$generated = date('M d, Y - h:i A');
			$filename = 'classlist_'.date('M-d,-Y_H-i-A');
			$this->printer->init('','','','',5,5,15,15,4,8)
						 ->footer($footer.' - Generated On '.$generated)
						 ->header($this->school_name)
						 ->generate($html,$filename);
		}

		show_404();
	}
	
	public function thisclasslist($hash = '')
	{
		$x = $this->hs->decrypt($hash);

		if(IS_AJAX)
		{

			if($x)
			{
				$this->load->model('M_sections','sec');
				$sec = $this->sec->get_all_sections();

				if($sec)
				{
					foreach($sec as $k => $v)
					{
						$section[__link($v->section_id)] = $v->section_name;
					}

					$result = ['stat'=>true,'data'=>$section];
				}else{
					$result = ['stat'=>false];
				}
			}else{
				$result = ['stat'=>false];
			}

			header('Content-Type: application/json');
			echo json_encode($result);
			exit();
		}else{
			if($x)
			{
				$this->load->model('M_sections','sec');

			}

			show_404();
		}
	}
	
	public function enrollmentsummary($what= '')
	{
		if(in_array(strtolower($what),['verified','unverified']))
		{
			$this->load->model('M_search','ms');
			$this->load->library('printer');
			if($what == 'verified')
			{
				$data['count'] = $this->ms->count_all_students_per_grade_level();
				$data['pre']['male'] = $this->ms->count_students_by_level_type('male','preschool');
				$data['pre']['female'] = $this->ms->count_students_by_level_type('female','preschool');
				$data['elem']['male'] = $this->ms->count_students_by_level_type('male',['intermediate','primary']);
				$data['elem']['female'] = $this->ms->count_students_by_level_type('female',['intermediate','primary']);
				$data['hs']['male'] = $this->ms->count_students_by_level_type('male','secondary');
				$data['hs']['female'] = $this->ms->count_students_by_level_type('female','secondary');
				$footer = 'All Students Verified by registrar';
			}else{
				$data['count'] = $this->ms->count_all_students_per_grade_level(TRUE);
				$data['pre']['male'] = $this->ms->count_students_by_level_type('male','preschool',TRUE);
				$data['pre']['female'] = $this->ms->count_students_by_level_type('female','preschool',TRUE);
				$data['elem']['male'] = $this->ms->count_students_by_level_type('male',['intermediate','primary'],TRUE);
				$data['elem']['female'] = $this->ms->count_students_by_level_type('female',['intermediate','primary'],TRUE);
				$data['hs']['male'] = $this->ms->count_students_by_level_type('male','secondary',TRUE);
				$data['hs']['female'] = $this->ms->count_students_by_level_type('female','secondary',TRUE);
				$footer = 'All Students Verified and Unverified by registrar';
			}
				$data['print'] = TRUE;
				$data['school_name'] = $this->school_name;
				$data['school_year'] = $this->schoolyear_date;
				$html = $this->load->view('search/_enrollment_summary',$data,TRUE);
				$generated = date('M d, Y - H:i A');
				$filename = 'Enrollment Summary '.$what.' ';
				$this->printer->init('','','','',5,5,8,3,4,3)
					 ->footer($footer.' - Generated On '.$generated)
					 ->header('Page {PAGENO} of {nbpg} ')
					 ->generate($html,$filename);
					 
				exit();
			
		}else{
			show_404();
		}
	}
	
	/***** LIBRARY REPORTS *****/

	public function media_type_master($id = false)
	{
		$this->load->model('M_library_books');

		set_time_limit(0);
		$data['school_name'] = $this->school_name;
		$data['school_address'] = $this->school_address;
		$data['type'] = $t = $this->M_library_books->get_master_list_by_type($id);
		
		$html = $this->load->view('circulation/pdf_media_type_master',$data,TRUE);
		// exit(0);
		$this->__create_pdf($html);
		exit(0);
	}

	public function media_category_master($id = false)
	{
		$this->load->model('M_library_books');

		set_time_limit(0);
		$data['school_name'] = $this->school_name;
		$data['school_address'] = $this->school_address;
		$data['type'] = $t = $this->M_library_books->get_master_list_by_category($id);
		
		$html = $this->load->view('circulation/pdf_media_category_master',$data,TRUE);
		// exit(0);
		$this->__create_pdf($html);
		exit(0);
	}

	public function all_library_media()
	{
		$this->load->model('M_library_books');

		set_time_limit(0);

		unset($get);
		$data['school_name'] = $this->school_name;
		$get['order'] = 'book_name';
		$get['fields'] = array(
				'library_books.*',
				'library_book_category.lbc_name as category',
				'book_copies - book_borrowed as available',
				'media_types.media_type',
		);

		$get['join'][] = array(
				'table' => 'media_types',
				'on' => 'library_books.media_type_id = media_types.id',
				'type' => 'left'
			);

		$get['join'][] = array(
				'table' => 'library_book_category',
				'on' => 'library_books.book_category = library_book_category.lbc_id',
				'type' => 'left'
			);
		
		$data['search'] = $this->M_library_books->get_record(false, $get);
		
		$html = $this->load->view('circulation/pdf_all_library_media',$data,TRUE);
		
		$this->__create_pdf($html);
		exit(0);
	}

	public function circulation_report()
	{

		if(!$_POST){
			redirect('circulation/circulation_report');
		}
		set_time_limit(0);
		$this->load->model(array(
				'M_media_types',
				'M_librarian',
				'M_student',
				'M_library_books',
				'M_enrollees',
				'M_library_book_category',
				'M_employees'
			));
		$this->load->model('M_lib_circulationfile1','m_lib_c1');
		$this->load->model('M_lib_circulationfile2','m_lib_c2');

		//Load dropdowns
		$this->view_data['media_types'] = $this->M_media_types->get_for_dd(array('id','media_type'), false, 'All Types','media_type');
		$this->view_data['book_category'] = $this->M_library_book_category->get_for_dd(array('lbc_id','lbc_name'), false, 'All Categories','lbc_name');

		$tab = $_POST ? $this->input->post('tab') : 'date';

		/** GENERATE REPORT BY BORROW DATE **/
		if($this->input->post('print_by_date')){
			
			$trndte = $this->input->post('trndte');

			$data = $rs = $this->m_lib_c2->circulation_report_by_date($trndte);
			$data['report_type'] = 'trndte';
			$data['orientation'] = $o = $trndte['orientation'];
		}

		/** GENERATE REPORT BY RETURN DATE **/
		if($this->input->post('print_by_ret_date')){
			
			$retdte = $this->input->post('retdte');

			$data = $rs = $this->m_lib_c2->circulation_report_by_retdte($retdte);

			$data['report_type'] = 'retdte';
			$data['orientation'] = $o = $retdte['orientation'];
		}

		/** GENERATE REPORT BY BORROWER **/
		if($this->input->post('print_by_borrower')){
			
			$borrower = $this->input->post('borrower');

			$data = $rs = $this->m_lib_c2->circulation_report_by_borrower($borrower);

			$data['report_type'] = 'borrower';
			$data['orientation'] = $o = $borrower['orientation'];
			$data['usertype'] = $borrower['usertype'];

		}

		/** GENERATE REPORT BY MEDIA **/
		if($this->input->post('print_by_media')){
			
			$media = $this->input->post('media');

			$data = $rs = $this->m_lib_c2->circulation_report_by_media($media);

			$data['report_type'] = 'media';
			$data['orientation'] = $o = $media['orientation'];

		}

		$data['school_name'] = $this->school_name;

		$html = $this->load->view('circulation/pdf_circulation_report',$data,TRUE);
		// exit(0);
		$this->__create_pdf($html,false, true, true, false,[10,10,20,20,15,15,[215.9,279.4]], $o);
		exit(0);
	}

	/***** END LIBRARY REPORTS *****/
}